﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SenerPro;

namespace MensUnoShop.Models
{
    public class MarketFactory
    {
        private static Logger _log = new Logger(Config.logpath);
        public static NormalMarket GetNormalMarket(Guid mkguid)
        {
            DBMarket dbm = new DBMarket();
            NormalMarket m = dbm.GetNormalMarketDetail(mkguid);
            return m;
        }

        public static NormalMarket GetNormalMarket(string mksn)
        {
            DBMarket dbm = new DBMarket();
            NormalMarket m = dbm.GetNormalMarketDetail(mksn);
            return m;
        }

        public static MagazineMarket GetMagazineMarket(Guid mkguid)
        {
            DBMarket dbm = new DBMarket();
            MagazineMarket m = dbm.GetMagazineMarketDetail(mkguid);
            return m;
        }

        public static MagazineMarket GetMagazineMarket(string mksn)
        {
            DBMarket dbm = new DBMarket();
            MagazineMarket m = dbm.GetMagazineMarketDetail(mksn);
            return m;
        }

        public static List<SimpleMarket> GetMarketList(long tagid)
        {
            DBMarket dbm = new DBMarket();
            List<SimpleMarket> smlist = dbm.GetMarketList(tagid);
            return smlist;
        }

        public static List<SimpleMarket> GetMarketListOpen(long tagid)
        {
            DBMarket dbm = new DBMarket();
            List<SimpleMarket> smlist = dbm.GetMarketList(tagid, DateTime.Now);
            return smlist;
        }

        public static List<SimpleMarket> GetMarketListFree(long c1id, long c2id)
        {
            DBMarket dbm = new DBMarket();
            List<SimpleMarket> smlist = dbm.GetMarketFreeList(c1id, c2id, DateTime.Now);
            return smlist;
        }

        public static List<SimpleMarket> GetMarketListCombo(long c1id, long c2id)
        {
            DBMarket dbm = new DBMarket();
            List<SimpleMarket> smlist = dbm.GetMarketFreeList(c1id, c2id, DateTime.Now);
            foreach (var s in smlist)
            {
                s.TYPE = Config.MarketType.Combo;
                s.GetMarketUri();
            }
            return smlist;
        }

        public static List<SimpleMarket> GetMarketList(long tagid, int amount)
        {
            DBMarket dbm = new DBMarket();
            List<SimpleMarket> smlist = dbm.GetMarketList(tagid, amount);
            return smlist;
        }

        public static List<SimpleMarket> GetMarketListOpen(long tagid, int amount)
        {
            DBMarket dbm = new DBMarket();
            List<SimpleMarket> smlist = dbm.GetMarketList(tagid, amount, DateTime.Now);
            return smlist;
        }

        public static bool FreeSpecStock(Guid _scguid)
        {
            DBMarket dbm = new DBMarket();
            return dbm.FreeSpecStock(_scguid);
        }

        public static bool FreeSpecStock(Guid _scguid, Guid mokguid)
        {
            DBMarket dbm = new DBMarket();
            return dbm.FreeSpecStock(_scguid,mokguid);
        }

        public static Guid CreateNewMarket(string MarketSN, string MarketTitle, long MarketTagid, int MarketShowprice, int MarketPrice,
                                            DateTime? MarketSdate,DateTime? MarketEdate, string MarketWashnote,string MarketShoppingnote,
                                            string MarketSpec, string MarketIntrodution, long grouptagid, long colortagid)
        {
            DBMarket dbm = new DBMarket();
            return dbm.CreateNewMarket( MarketSN,  MarketTitle,  MarketTagid,  MarketShowprice,  MarketPrice,MarketSdate,MarketEdate,  
                                        MarketWashnote,MarketShoppingnote,MarketSpec,  MarketIntrodution,  grouptagid,  colortagid, Config.MarketType.Normal);
        }

        public static Guid CreateNewMagazineMarket(string MarketSN, string MarketTitle, long MarketTagid, int MarketShowprice, int MarketPrice,
                                            DateTime? MarketSdate, DateTime? MarketEdate, string MarketIntrodution)
        {
            DBMarket dbm = new DBMarket();
            long grouptagid = TagFactory.CheckTag("N/A", Config.TagType.GROUP);
            long colortagid = TagFactory.CheckTag("N/A", Config.TagType.COLORGOURP);
            return dbm.CreateNewMarket(MarketSN, MarketTitle, MarketTagid, MarketShowprice, MarketPrice, MarketSdate, MarketEdate,
                                        string.Empty, string.Empty, string.Empty, MarketIntrodution, grouptagid, colortagid, Config.MarketType.Mangazine);
        }

        public static Guid CreateNewProduct(Guid mkguid, long ProductColorTagid, string ProductBig, string ProductSmall)
        {
            DBMarket dbm = new DBMarket();
            return dbm.CreateNewProduct(mkguid,ProductColorTagid, ProductBig, ProductSmall);
        }

        public static Guid CreateNewSpec(Guid pdguid, long titletagid, int stock)
        {
            DBMarket dbm = new DBMarket();
            return dbm.CreateNewSpec(pdguid, titletagid, stock);
        }

        public static void ImportMarketPic(string mksn, string picpath, string picnote)
        {
            DBMarket dbm = new DBMarket();
            dbm.ImportMarketPic(mksn, picpath, picnote);
        }

        public static void UpdateMarket(Guid mkguid, string MarketTitle, long MarketTagid, int MarketShowprice, int MarketPrice, DateTime? MarketSdate, DateTime? MarketEdate, 
            string MarketWashnote, string MarketShoppingnote, string MarketSpec, string MarketIntrodution, long grouptagid, long colortagid)
        {
            DBMarket dbm = new DBMarket();
            dbm.UpdateMarket(mkguid, MarketTitle, MarketTagid, MarketShowprice, MarketPrice, MarketSdate, MarketEdate,
                                MarketWashnote, MarketShoppingnote, MarketSpec, MarketIntrodution, grouptagid, colortagid);
        }

        public static Guid GetProductId(Guid specid)
        {
            DBMarket dbm = new DBMarket();
            return dbm.GetProductId(specid);
        }

        public static void CreateMarketPic(Guid mkguid, string note, string pic, int sequence)
        {
            DBMarket dbm = new DBMarket();
            dbm.CreateMarketPic(mkguid, note, pic, sequence);
        }


        public static IMarket GetMarket(Guid mkguid, Config.MarketType marketType)
        {
            IMarket im = null;
            switch (marketType)
            { 
                case Config.MarketType.Normal:
                    im = GetNormalMarket(mkguid);
                    break;
                case Config.MarketType.Mangazine:
                    im = GetMagazineMarket(mkguid);
                    break;
                case Config.MarketType.Combo:
                    im = GetNormalMarket(mkguid);
                    im.TYPE = Config.MarketType.Combo;
                    break;
            }
            return im;
        }

        public static List<ManagerSimpleMarket> GetMarketListForAdmin(long tagid, string query)
        {
            DBMarket dbm = new DBMarket();
            List<ManagerSimpleMarket> smlist = dbm.GetMarketListForAdmin(tagid, query, false);
            return smlist;
        }

        public static List<ManagerSimpleMarket> GetMagaListForAdmin(long tagid, string query)
        {
            DBMarket dbm = new DBMarket();
            List<ManagerSimpleMarket> smlist = dbm.GetMarketListForAdmin(tagid, query, true);
            return smlist;
        }

        public static bool UpdateMarketStatus(Guid mkguid)
        {
            DBMarket dbm = new DBMarket();
            return dbm.UpdateMarketStatus(mkguid);
        }

        
        public static MarketImage GetMarketPicture(int mpid)
        {
            DBMarket dbm = new DBMarket();
            return dbm.GetMarketImage(mpid);
        }

        public static void UpdateMarketImage(MarketImage mi)
        {
            DBMarket dbm = new DBMarket();
            dbm.UpdateMarketImage(mi);
        }

        public static List<TinyMarket> GetTempComboMarketList(long tagid)
        {
            string pid = "tempcombolist_" + tagid;
            List<TinyMarket> templist = new List<TinyMarket>();
            if (HttpContext.Current.Session[pid] != null)
                templist = (List<TinyMarket>)HttpContext.Current.Session[pid];
            return templist;
        }



        public static void SetTempComboMarketListIn(TinyMarket tm)
        {
            string pid = "tempcombolist_" + tm.COMBOID;
            List<TinyMarket> templist = new List<TinyMarket>();
            if (HttpContext.Current.Session[pid] != null)
                templist = (List<TinyMarket>)HttpContext.Current.Session[pid];
            templist.Add(tm);
            HttpContext.Current.Session[pid] = templist.OrderBy(t=>t.MKGUID).ThenBy(t=>t.SPECID).ToList();
        }

        public static void SetTempComboMarketListOut(long tagid, string mkguid, string specid)
        {
            string pid = "tempcombolist_" + tagid;
            List<TinyMarket> templist = new List<TinyMarket>();
            if (HttpContext.Current.Session[pid] != null)
                templist = (List<TinyMarket>)HttpContext.Current.Session[pid];
            templist.Remove(templist.FirstOrDefault(t=>t.MKGUID==new Guid(mkguid)&&t.SPECID==new Guid(specid)));
            HttpContext.Current.Session[pid] = templist.OrderBy(t => t.MKGUID).ThenBy(t => t.SPECID).ToList();
        }

        public static bool CheckComboMarketList(long id, ref string msg)
        {
            bool ischeck = false;
            string pid = "tempcombolist_" + id;
            List<TinyMarket> templist = new List<TinyMarket>();
            if (HttpContext.Current.Session[pid] != null)
                templist = (List<TinyMarket>)HttpContext.Current.Session[pid];

            if (templist.Count > 0)
            {
                USalesKit sk = SalesKitFactory.GetCombo(id);
                int pices, comboprice;
                if (int.TryParse(sk.SSnote.Split('|')[0], out pices) && int.TryParse(sk.SSnote.Split('|')[1], out comboprice))
                {
                    if (templist.Count % pices == 0)
                        ischeck = true;
                    else
                        msg = string.Format("還差{0}件，請再繼續選購!!",(pices-(templist.Count % pices)).ToString());
                }
                else
                {
                    msg = "設定錯誤!!請連絡網站管理員!";
                }
            }
            else
            {
                msg = "請新增特惠組合!!";
            }
            return ischeck;
        }

        
    }
}